/**
 * 
 */
package com.cipe.cmrs.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Incepio
 */
public enum DateUtil2 {
    INSTANCE;

    public static final int ONE_HOUR = 1000 * 60 * 60;

    public static final String DEFAULT_PATTERN = "MM/dd/yyyy";

    public String formatDate(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_PATTERN);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return formatter.format(cal.getTime());
    }

    public String formatDate(Timestamp timestamp) {
        if (timestamp == null) {
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(DEFAULT_PATTERN);
        Calendar cal = Calendar.getInstance();
        cal.setTime(timestamp);
        return formatter.format(cal.getTime());
    }

    public String formatDate(String pattern, Date date) {
        if (date == null || pattern == null) {
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return formatter.format(cal.getTime());
    }

    public Date getSodDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    public Date getEodDate(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }

    /**
     * Get number of days between start and end dates.
     * 
     * @param startDate
     * @param endDate
     * @return int as count of number of days in between the specified date
     *         parameters.
     */
    public int getDateInterval(Date startDate, Date endDate) {
        Calendar start = Calendar.getInstance();
        start.setTime(startDate);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);
        start.set(Calendar.AM_PM, Calendar.AM);

        Calendar end = Calendar.getInstance();
        end.setTime(endDate);
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 999);
        start.set(Calendar.AM_PM, Calendar.PM);
        
        long diff = end.getTimeInMillis() - start.getTimeInMillis();

        return (int) (diff / (ONE_HOUR * 24));
    }

    public boolean isSameDay(Date date1, Date date2) {
        return date1 == null && date2 == null ? true
            : date1 != null && date2 != null ? getSodDate(date1).compareTo(getSodDate(date2)) == 0 : false;
    }

    public boolean isBetween(Date dateToCheck, Date start, Date end) {
        return dateToCheck.compareTo(start) >= 0 && dateToCheck.compareTo(end) <= 0;
    }
}
